package utils
{
	import flash.display.Sprite;
	import flash.geom.Point;

	/**
	 * 关节
	 * @author sjp
	 *
	 */
	public class Segment extends Sprite
	{
		private var color:uint;
		private var segmentWidth:Number;
		private var segmentHeight:Number;
		public var vx:Number = 0;
		public var vy:Number = 0;

		public function Segment(segmentWidth:Number, segmentHeight:Number, color:uint = 0xffffff)
		{
			this.segmentWidth = segmentWidth;
			this.segmentHeight = segmentHeight;
			this.color = color;
			init();
		}

		public function init():void
		{ // 绘制关节 
			graphics.lineStyle(0);
			graphics.beginFill(color);
			graphics.drawRoundRect(-segmentHeight / 2, -segmentHeight / 2, segmentWidth + segmentHeight, segmentHeight, segmentHeight, segmentHeight);
			graphics.endFill();
			// 绘制两个“枢轴” 
			graphics.drawCircle(0, 0, 2);
			graphics.drawCircle(segmentWidth, 0, 2);
		}

		/**
		 * 返回一个 flash.geom.Point 的实例。它将返回右侧枢轴的 x,y 坐标。
		 * @return
		 *
		 */
		public function getPin():Point
		{
			var angle:Number = rotation * Math.PI / 180;
			var xPos:Number = x + Math.cos(angle) * segmentWidth;
			var yPos:Number = y + Math.sin(angle) * segmentWidth;
			return new Point(xPos, yPos);
		}
	}
}